package com.steakliu.gateway.routes;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.steakliu.gateway.dto.RouteDto;
import com.steakliu.gateway.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.*;
@Component
public class MySQLRouteDefinitionRepository implements RouteDefinitionRepository {
    @Autowired
    private RouteService routeService;

    private List<RouteDefinition> routeDefinitions = new ArrayList<>();

    @Override
    public Flux<RouteDefinition> getRouteDefinitions() {
        routeDefinitions.clear();
        List<RouteDto> list = routeService.list();
        List<Route> routes = new ArrayList<>();
        list.forEach(routeDto -> {
            Route route = new Route();
            route.setId(routeDto.getRouteId())
                    .setOrder(routeDto.getOrder())
                    .setUri(routeDto.getUri())
                    .setFilters(new ArrayList<>(1))
                    .setPredicates(new ArrayList<>(2));
            routes.add(route);
        });
        routes.forEach(route -> {
            routeDefinitions.add(JSON.parseObject(JSON.toJSONString(route),RouteDefinition.class));
        });
        return Flux.fromIterable(routeDefinitions);
    }

    @Override
    public Mono<Void> save(Mono<RouteDefinition> route) {
        return null;
    }

    @Override
    public Mono<Void> delete(Mono<String> routeId) {
        return null;
    }
}
